package com.youyunsoft.commons;

import android.os.Bundle;
import android.os.Handler;
import androidx.annotation.IdRes;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;

public abstract class BaseActivity extends AppCompatActivity {

   protected Handler mHandler = new Handler();

   protected  String TAG=this.getClass().getSimpleName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setTitle(this.getClass().getSimpleName());
            actionBar.setDisplayHomeAsUpEnabled(true);
        }

    }

    public <T extends View> T bindView(@IdRes int resId){
        return (T)super.findViewById(resId);
    }
    public <T extends View> T bindView(View layoutView, @IdRes int resId){
        return (T)layoutView.findViewById(resId);
    }


    protected void ToastMsg(Object object) {
        if (object != null) {
            Toast.makeText(this, object.toString(), Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {

            case android.R.id.home:
                finish();
                break;

        }

        return super.onOptionsItemSelected(item);
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        mHandler.removeCallbacksAndMessages(null);
    }
}
